﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using EShop.Common;
using EShop.BussinessObjects;

namespace EShop.DataAccess
{
    public class CDAShape
    {

        public static List<Shape> Select(Shape shape)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            con.Open();

            string _cmdText =
                 "SELECT s.* " +
                 "FROM shape s " +
                 "WHERE ((s.ID = " + shape.ID.ToString() + ") OR (" + shape.ID.ToString() + " = " + ((int)cnstDatabase.DBOperationResult.ValueIsNotEffective).ToString() + "))" +
                 "ORDER BY s.ID";
            
            SqlCommand cmd = new SqlCommand(_cmdText, con);
            SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
            DataTable tbl = new DataTable();
            dataAdapter.Fill(tbl);

            List<Shape> _shapeList = new List<Shape>();

            foreach (DataRow row in tbl.Rows)
            {
                Shape _shape = new Shape();
                _shape.ID = (row["ID"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToInt32(row["ID"]));
                _shape.Name = (row["Name"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue).ToString() : Convert.ToString(row["Name"]));

                _shapeList.Add(_shape);
            }

            con.Close();
            return _shapeList;
        }
    }
}